 window.fbAsyncInit = function() {
  FB.init({
    appId      : '601280189947963',
    frictionlessRequests : true,
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    oauth      : true,
    xfbml      : true  // parse XFBML
    
  });
    
   var admin_accout='';
   var admin_password='';
  // Here we subscribe to the auth.authResponseChange JavaScript event. This event is fired
  // for any authentication related change, such as login, logout or session refresh. This means that
  // whenever someone who was previously logged out tries to log in again, the correct case below 
  // will be handled. 
  
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
      // The response object is returned with a status field that lets the app know the current
      // login status of the person. In this case, we're handling the situation where they 
      // have logged in to the app.
    	
      testAPI();
    } else if (response.status === 'not_authorized') {
      // In this case, the person is logged into Facebook, but not into the app, so we call
      // FB.login() to prompt them to do so. 
      // In real-life usage, you wouldn't want to immediately prompt someone to login 
      // like this, for two reasons:
      // (1) JavaScript created popup windows are blocked by most browsers unless they 
      // result from direct interaction from people using the app (such as a mouse click)
      // (2) it is a bad experience to be continually prompted to login upon page load.
       FB.login(function(response) {}, {scope: ['email','user_likes','create_event','manage_pages']});
    } else {
      // In this case, the person is not logged into Facebook, so we call the login() 
      // function to prompt them to do so. Note that at this stage there is no indication
      // of whether they are logged into the app. If they aren't then they'll see the Login
      // dialog right after they log in to Facebook. 
      // The same caveats as above apply to the FB.login() call here.
    	FB.login(function(response) {}, {scope: ['email','user_likes','create_event','manage_pages']});
    }
  });
  };

  // Load the SDK asynchronously
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));

  // Here we run a very simple test of the Graph API after login is successful. 
  // This testAPI() function is only called in those cases. 
  function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me?fields=picture,name,email', function(response) {
      console.log(response);
      userID = response.id;
      user = response;
      console.log('Good to see you, ' + response.name + '.'+response.email);
      searchEventToServlet();
    });
  }

  function getpermission(){
	  FB.login(function(response) {}, {scope: ['email','user_likes','create_event','manage_pages']});
  }
    function postFeed(){
              var args = {};
                
                //args.picture = "http://www.allenj.net/a.jpg";
                //args.link = "http://www.allenj.net";

                args.description = "Test";
                 
                 for(var i=0;i<5;i++)
                 {
                    args.message = "Test"+i;
                    FB.api('/me/feed', 'post', args, function(response) {
                        if (!response || response.error) {
                            alert('error');
                        } else {
                            alert('Post ID: ' + response.id);
                        }
                    });
                }
    }
    function userCreateEvent(){
        //https://developers.facebook.com/docs/graph-api/reference/user/events/
        
        alert('create evnet');
        FB.api(
            "/me/events",
            "POST",
            {
                "name": "This is a test event",
                "start_time": "2014-03-01T13:00"
                /*end_time
                 * description
                 * location
                 * location_id����
                 * privacy_type:enum{'OPEN', 'SECRET', 'FRIENDS'}
                 */
            },
            function (response) {
              if (response && !response.error) {
                /* handle the result */
               console.log(response);//return event ID
              }
              else
                alert('error');
            }
        );
    }
    //FB.api("/fql?q={your urlencoded query}", callback() { ��} );  //FQL query
    function accountInfo(){
        //https://developers.facebook.com/docs/graph-api/reference/user/events/
        FB.api(
            "/me/pages",
            
            function (response) {
              if (response && !response.error) {
                /* handle the result */
               console.log(response);//return event ID
              }
              else
                alert('error');
            }
        );
    }
   function getUserData(id,datas,i)
   {
       FB.api(
           "/"+id+"?fields=picture,name",
           function(response)
           {
               userCompleteCount++;
               if(response&&!response.error)
               {
                   datas[i] = response;
                   console.log('get');
               }
               else
               console.log(response);
             if(userCompleteCount==commentNum)
               getCommentFBUserComplete();
               console.log(userCompleteCount+","+commentNum);
           }
       )
   }
    

    function getUserCreatedEvent()//敺雿輻���萄遣�暑��
    {
         FB.api(
            "/me/event",
            function (response) {
              if (response && !response.error) {
                /* handle the result */
               console.log(response);
               //send to database to save
               
              }
              else
               console.log('error');
            }
        );
    } 
    function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

//post_to_url('http://example.com/', {'id':'1409056866000177'});
